 #This is the file to be sourced from R. Its sets up some of the 
 #nessesary data for the simulation and then start the simulation
 
 require("R.oo") #Package R.oo is used to bring object oriented design 
				 #to R which helps organize and structurthe simulation 

#Point the path to where the files reside
rootdir = file.path("C:", "users","owner", "Desktop", "Ragent", "trunk" )
if ( Sys.info()["sysname"] == "Linux"  ) rootdir = file.path("~",
"projects", "crab.ibm.benthic", "src" )
environ = file.path( rootdir, c("AEnvironment.r", "RAgent.r", 
"ATemp.r", "ADepth.r", "ABottom.r", "ACell.r", "ASnowCrab.r", "ASCmale.r" ) )
for (e in environ) source(e)
 



 
##READ IN REAL MOVEMENT DATA. May be later declared in a crab movement object

#This creates the displacement density function of crab movement
#taken from real tagging data. For now it can be referenced by crab 
#agents by calling mfunc 
dat = read.csv(file.path( rootdir, c("DispDays.txt"))) #Load CSV file with tagging movement data (Displacement in Nautical miles, days) 
dat$disp = dat$disp*1.852 #Displacement in km to fit properly with the grid units
vec = dat$disp/dat$days # vec is a vector of the displacement per day for each capture
nbins = 100 
bks = seq( from=min(vec), to=max(vec), length.out=nbins )
ohist = hist(vec, breaks = bks, plot = F)
pden = ohist$counts/sum(ohist$counts) # Fix densitys for percent of total densities
cdf = cumsum(pden) 
mids = ohist$mids
cdf[nbins] <- 0 # Add a zero to allow approximation of values between 0 and min density
mids[nbins] <- 0 # Add zero to allow approximation less than first bin value
mfunc = approxfun(x = cdf, y = mids) #approximate the function

#This creates the directional density function of crab movement
#taken from real tagging data. For now it can be referenced by crab 
#agents by calling bfunc. This function does not follow any noticable
#curve. 
dat = read.csv(file.path( rootdir, c("Azimuths.txt"))) #Load CSV file of Azimuths
nbins = 90
bks = seq( from=min(dat$bearing), to=max(dat$bearing), length.out=nbins )
ohist = hist(dat$bearing, breaks = bks, plot = F)
pden = ohist$counts/sum(ohist$counts)
cdf = cumsum(pden) 
mids = ohist$mids
cdf[nbins] <- 0 # Add a zero to allow approximation of values between 0 and min density
mids[nbins] <- 0 # Add zero to allow approximation less than first bin value
bfunc = approxfun(x = cdf, y = mids) 
 


#Declare an environment object
env = AEnvironment()
#Include in the object the listed Environmental data. Further environmental
#  objects can be defined within AEnvironment.r
env$includeEnvironmentals(c("depth", "temp"))
#List the agents that will be included in the simulation. Further agents 
#  can be defined in AEnvironment.r 
env$includeAgents(c("SnowCrab"))
#Creates the outer boundary of the environment 
bndary = matrix(c(0, 0, 100, 100, 0,  0, 100, 100, 0, 0), ncol = 2, nrow = 5)
#Set the geography (Boundary) of the environment
env$setgeo(bndary)
#Define the resolution of environment cell divisions
celwid = 10
celhei = 10
#Generate the cells of the environment
env$generateCells(c(celwid, celhei))#10 Km by 10 Km
#The environment has now been set up with all desired agents, cells, and 
#environmental data. 

#Call to begin the simulation. Can set number of days to run
env$beginsim(ndays = 5) 